package com.yyys.web.service.impl;

import com.yyys.web.entity.GoodsInfo;
import com.yyys.web.json.Goods;
import com.yyys.web.repository.GoodsRepository;
import com.yyys.web.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengsenwen on 2017/8/4.
 */

@Service
public class GoodsServiceImpl implements GoodsService {
    private Logger logger = Logger.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsRepository goodsRepository;

    @Override
    public List<Goods> getByCTIDAndGoodsTypeName(String ctid) {

        List<Goods> goodsList = new ArrayList<>();
        List<GoodsInfo> goodsInfoList = new ArrayList<>();

        List<String> typeNameList = goodsRepository.findgoodsTypeNameById(ctid);
        logger.info("typeNameList: list is " + typeNameList);

        for (String str : typeNameList) {
            Goods goods = new Goods();
            goods.setTypeName(str);

            for (int i = 0; i < typeNameList.size(); i++) {
                goodsInfoList = goodsRepository.findByCTIDAndGoodsTypeName(ctid, str);
                logger.info("goodsInfoList: list is " + goodsInfoList);

                if (goodsInfoList != null) {
                    goods.setGoodsInfoList(goodsInfoList);
                }
            }

            goodsList.add(goods);
        }

        return goodsList;
    }

    @Override
    public GoodsInfo getByGoodsId(String goodsId) {
        return goodsRepository.findByGoodsId(goodsId);
    }

    @Override
    public GoodsInfo getByCTIdAndGoodsId(String ctid, String goodsId) {
        return goodsRepository.findByCtidAndGoodsId(ctid, goodsId);
    }
}
